<?php
/**
* 邮件相关逻辑
*
* @author     Robot
* @version    1.0
* @package    Base Framework
* @access     public
* @copyright  商业软件,受著作权保护
* @link       http://***
*/
class mailModule extends Base
{
	/**
	 * 构造函数
	 *
	 */
	public function __construct(){}

	/**
	* 发邮件
	*
	* @access public
	* @param  void
	* @return void
	*/
	public function sendmail($to, $arr)
	{
		$fileValue = file_get_contents(CONFIG_DIR."mail/mail_header.txt");
		$fileValue .= file_get_contents(CONFIG_DIR."mail/mail_xuanyou.txt");
		$fileValue = str_replace("{username}",substr($to, 0, strpos($to, '@')),$fileValue);
		foreach($arr['result'] as $result)
		{
			$tmp .= iconv("UTF-8","gbk","　　　　".(++$i).".【".$result."】\n");
			$title .= "【".$result."】";
		}
		$fileValue = str_replace("{result}", $tmp, $fileValue);
		foreach($arr['postcontent'] as $key => $val)
		{
			$fileValue = str_replace("{".$key."}",iconv("UTF-8","gbk",$val),$fileValue);
		}

		if($arr["type"]>0&&!empty($arr['nextpost'])){
			switch ($arr["type"]) {
				case 1:
					$fileValue .= file_get_contents(CONFIG_DIR."mail/mail_xuanyou_1.txt");
					break;
				case 2:
					$fileValue .= file_get_contents(CONFIG_DIR."mail/mail_xuanyou_2.txt");
					break;
				case 3:
					$fileValue .= file_get_contents(CONFIG_DIR."mail/mail_xuanyou_3.txt");
					break;
				case 4:
					$fileValue .= file_get_contents(CONFIG_DIR."mail/mail_xuanyou_4.txt");
					break;
				case 5:
					$fileValue .= file_get_contents(CONFIG_DIR."mail/mail_xuanyou_5.txt");
					break;
				case 6:
					$fileValue .= file_get_contents(CONFIG_DIR."mail/mail_xuanyou_6.txt");
					break;
			}
			foreach ($arr['nextpost'] as $key => $value) {
				$fileValue = str_replace("{".$key."}",iconv("UTF-8","gbk",$value),$fileValue);
			}
		}
		//过滤变量
		$fileValue = preg_replace("/{.*}/",iconv("UTF-8","gbk",""),$fileValue);
		$fileValue = preg_replace("/<%.*%>/",iconv("UTF-8","gbk",""),$fileValue);
		$fileValue = str_replace("</li>",iconv("UTF-8","gbk","\n"),$fileValue);
		$fileValue = str_replace("<li>",iconv("UTF-8","gbk","    "),$fileValue);
		$fileValue = str_replace("<ul>",iconv("UTF-8","gbk",""),$fileValue);
		$fileValue = str_replace("</ul>",iconv("UTF-8","gbk",""),$fileValue);
		$fileValue .= file_get_contents(CONFIG_DIR."mail/mail_footer.txt");
		Common::load("MailSmtp");
		$subject = iconv("UTF-8","gbk","选油报告：可选用".$title."");
		$smtp = new MailSmtp(MAIL_HOST,25,true,MAIL_USER,MAIL_PASS);
		//$smtp->show_errors = true;
		return $smtp->send_mail($to,MAIL_USER, $subject, $fileValue, "TXT");
	}

	public function sendAdminMail($arr,$post)
	{
		$fileValue = file_get_contents(CONFIG_DIR."mail/mail_xuanyou.txt");
		foreach($arr['postcontent'] as $key => $val)
		{
			$fileValue = str_replace("{".$key."}",iconv("UTF-8","gbk",$val),$fileValue);
		}

		if($arr["type"]>0&&!empty($arr['nextpost'])){
			switch ($arr["type"]) {
				case 1:
					$fileValue .= file_get_contents(CONFIG_DIR."mail/mail_xuanyou_1.txt");
					break;
				case 2:
					$fileValue .= file_get_contents(CONFIG_DIR."mail/mail_xuanyou_2.txt");
					break;
				case 3:
					$fileValue .= file_get_contents(CONFIG_DIR."mail/mail_xuanyou_3.txt");
					break;
				case 4:
					$fileValue .= file_get_contents(CONFIG_DIR."mail/mail_xuanyou_4.txt");
					break;
				case 5:
					$fileValue .= file_get_contents(CONFIG_DIR."mail/mail_xuanyou_5.txt");
					break;
				case 6:
					$fileValue .= file_get_contents(CONFIG_DIR."mail/mail_xuanyou_6.txt");
					break;
			}
			foreach ($arr['nextpost'] as $key => $value) {
				$fileValue = str_replace("{".$key."}",iconv("UTF-8","gbk",$value),$fileValue);
			}
		}
		//过滤变量
		$fileValue = preg_replace("/{.*}/",iconv("UTF-8","gbk",""),$fileValue);
		$fileValue = preg_replace("/<%.*%>/",iconv("UTF-8","gbk",""),$fileValue);
		$fileValue = str_replace("</li>",iconv("UTF-8","gbk","\n"),$fileValue);
		$fileValue = str_replace("<li>",iconv("UTF-8","gbk","    "),$fileValue);
		$fileValue = str_replace("<ul>",iconv("UTF-8","gbk",""),$fileValue);
		$fileValue = str_replace("</ul>",iconv("UTF-8","gbk",""),$fileValue);
		$str = "";
		unset($post["id"]);
		foreach ($post as $key => $value) {
			$str .= "{$key}：{$value} \n";
		}
		$fileValue .= "\n".iconv("UTF-8","gbk",$str);
		Common::load("MailSmtp");
		$subject = iconv("UTF-8","gbk","选油报告：可选用".$title."");
		$smtp = new MailSmtp(MAIL_HOST,25,true,MAIL_USER,MAIL_PASS);
		//$smtp->show_errors = true;
		return $smtp->send_mail("cooljun@126.com",MAIL_USER, $subject, $fileValue, "TXT");
	}

	/**
	 * 记录日志
	 * 
	 */
	public function add($id, $mail)
	{
		$oDto = new DTO_User_Maillogs();
		$oDto->infoid = $id;
		$oDto->mail = $mail;
		$oDto->createtime = time();

		$oDmo = new DMO_User_Maillogs();

		if(!$oDmo->add($oDto))
		{
			throw new MyException("Err001");
		}

		return $oDmo->getInsertId();
	}

	/**
	* 列出日志
	*
	* @access public
	* @param  void
	* @return void
	*/
	public function listlog($aKey, $oPage)
	{
		$aOrder = array("id"=>"desc");

		$oDmo = new DMO_User_Maillogs();
		$aRes = $oDmo->getPage($oPage->getBegin(), $oPage->PerPage, $aKey, $aOrder);
		$oPage->Count = $oDmo->getTotal();

		return $aRes;
	}

}

?>